﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace mamnontuoitho7.AppCode
{
    public class ImageAlbum
    {
        public ConnectGetData _connectData;
        public ImageAlbum()
        {
            _connectData = new ConnectGetData();
         }

        /// <summary>
        /// insert image record for a album
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="ImageName"></param>
        /// <returns></returns>
        public bool ImageInsert(String productID, String ImageName)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sqlUp = "Insert into Image (AlbumID, ImageName) values(@AlbumID, @ImageName)";
                SqlCommand cmd = new SqlCommand(sqlUp, con);

                cmd.Parameters.AddWithValue("@AlbumID", productID);
                cmd.Parameters.AddWithValue("@ImageName", ImageName);
               
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {

                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// get image for a album
        /// </summary>
        /// <param name="productID"></param>
        /// <returns> datatable</returns>
        public DataTable GetImageByProductID(String productID)
        {
            SqlConnection con = _connectData.GetConnect();
          //  con.Open();
            try
            {
                string sql = "select * from Image where AlbumID= @ProductID ORDER BY ImageID DESC";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ProductID", productID);

                DataTable dt = _connectData.GetDataTable(cmd);
       //         con.Close();
                return dt;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return null;
            }

        }

        /// <summary>
        /// delete a image in a album
        /// </summary>
        /// <param name="ProductID"></param>
        /// <returns></returns>
        public bool ImageDelete(String imageID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "delete from Image  where ImageID = @ImageID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ImageID", imageID);


                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }

        /// <summary>
        /// get some latest image from Image table ex : "hình hảnh" trang chủ
        /// </summary>
        /// <returns></returns>
        public DataTable GetSomeImage(String amount)
        {
            string str = "select top(" + amount + ") AlbumID,ImageName from Image,Product where Product.ProductStatusID =0 and Product.ProductID = [Image].AlbumID ORDER BY ImageID DESC";
            return _connectData.GetDataTable(str);

        }
    }
}